package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.mysoft.budgetsoft.system.dao.MenuDao;
import net.mysoft.budgetsoft.system.dao.OrgDao;
import net.mysoft.budgetsoft.system.dao.RoleDao;
import net.mysoft.budgetsoft.system.dao.UserDao;
import net.mysoft.budgetsoft.system.domain.PageData;
import net.mysoft.budgetsoft.system.domain.TMenu;
import net.mysoft.budgetsoft.system.domain.TOrg;
import net.mysoft.budgetsoft.system.domain.TRole;
import net.mysoft.budgetsoft.system.domain.TUser;
import net.mysoft.budgetsoft.system.service.RoleService;
import net.mysoft.framework.util.Lists;
import net.mysoft.framework.util.Maps;
import net.mysoft.framework.util.Tool;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 角色service
 * @author kangrui
 *
 */
@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private MenuDao menuDao;
	@Autowired
	private OrgDao orgDao;
	@Autowired
	private UserDao userDao;

	@Override
	public PageData<TRole> getRoleDataGrid(Map map) {
		int pageNo = Tool.toInt(map.get("page"));
		int pageSize = Tool.toInt(map.get("rows"));
		PageData<TRole> page = new PageData<TRole>(pageNo, pageSize);
		page.setRows(roleDao.queryPageByHql("select new map(id as id, name as name, remark as remark, createDate as createDate)from TRole", pageNo, pageSize));
		return page;
	}

	@Override
	public void addRole(TRole role) {
		role.setCreateDate(new Date());
		roleDao.save(role);
	}

	/**
	 * 删除多个角色
	 * @param para
	 */
	@Override
	public void delRoles(String para) {
		if(StringUtils.isNotBlank(para)) {
			String[] ids = para.split(",");
			for(String id:ids) {
				TRole role = roleDao.load(TRole.class, id);
				roleDao.delete(role);
			}
		}
	}

	@Override
	public List getMenuTree(String id) {
		TRole role = roleDao.load(TRole.class, id);
		List role_menuids = Lists.makeListByProp(role.getMenus(), "id");
		List<TMenu> menus = menuDao.find("from TMenu");
		List list = new ArrayList();
		for(TMenu menu:menus) {
			Map map = new Maps().put("id", menu.getId()).put("text", menu.getName()).build();
			if(menu.getParent()!=null) map.put("pid", menu.getParent().getId());
			if(role_menuids.contains(menu.getId())) map.put("checked", true);
			else map.put("checked", false);
			list.add(map);
		}
		return list;
	}

	@Override
	public void saveRoleMenu(String roleId, String menuIds) {
		TRole role = roleDao.load(TRole.class, roleId);
		List<TMenu> menus = new ArrayList<TMenu>();
		if(StringUtils.isNotBlank(menuIds)) {
			String[] ids = menuIds.split(",");
			for(String id:ids) {
				TMenu menu = menuDao.load(TMenu.class, id);
				menus.add(menu);
			}
		}
		role.setMenus(menus);
	}

	@Override
	public List getOrgTree() {
		List<TOrg> orgs = orgDao.find("from TOrg");
		List tree = new ArrayList();
		for(TOrg org:orgs) {
			Map map = new Maps().put("id", org.getId()).put("text", org.getName()).build();
			if(org.getParent()!=null) map.put("pid", org.getParent().getId());
			tree.add(map);
		}
		return tree;
	}

	@Override
	public PageData getUserDataGrid(String orgId) {
		List<TUser> users = userDao.find("select new map(id as id, name as name, job as job"
				+ ", sex as sex) from TUser where org.id = ?", new Object[]{orgId});
		PageData<TUser> page = new PageData<TUser>();
		page.setRows(users);
		return page;
	}

	@Override
	public void saveRoleUser(String roleId, String userIds) {
		if(StringUtils.isNotBlank(userIds)) {
			String[] ids = userIds.split(",");
			List<TUser> users = new ArrayList<TUser>();
			for(String id:ids) {
				TUser user = userDao.load(TUser.class, id);
				users.add(user);
			}
			TRole role = roleDao.load(TRole.class, roleId);
			role.setUsers(users);
			roleDao.save(role);
		}
	}

}
